#!/usr/bin/env python

import wsgiref.handlers
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api.labs import taskqueue
from schema import RankComputation

class GraphMaker(webapp.RequestHandler):
  def get(self):
    body = '<form method="POST"><input type="submit" value="Graph" /></form>'
    self.response.out.write("<html><body>%s</body></html>" % body)

  def post(self):

    # Create an object to keep track of things
    computation = RankComputation()
    computation_key = computation.put()

    # Create a link graph
    task = taskqueue.Task(
      url='/find-fetches-to-parse',
      params={
        'computationid': computation_key.id(),
      }
    )
    task.add(queue_name='parse')

    # Collate the link graph
    body = '<form action="/collate-links" method="POST">'
    body += '<input type="hidden" name="computationid" value="'
    body += str(computation_key.id()) + '" />'
    body += '<input type="submit" value="Collate" /></form>'
    self.response.out.write("<html><body>%s</body></html>" % body)

def main():
  wsgiref.handlers.CGIHandler().run(webapp.WSGIApplication([
    ('/graph', GraphMaker),
  ]))

if __name__ == '__main__':
  main()
